<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/11/4
  Time: 16:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>退货</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            getSupplierDrop();
        })
        function getSupplierDrop(){
            $.ajax({
                url:"/supplier/getSupplier",
                dataType:"json",
                success(res){
                    for (let o of res) {
                        $("#supplierId").append(`
                            <option value="\${o.supplierId}" data-aa=\${o.remarks}>\${o.supplierName}</option>
                        `)
                    }
                },
                error(){
                    alert("服务器错误")
                }
            })
        }
    </script>
</head>
<body>
<table>
    <tr>
        <td>单号</td>
        <td>
            <span id="returnNumber"></span>
        </td>
        <td>供应商</td>
        <td>
            <select id="supplierId" onchange="getBangDinag()">
                <option value="">请选择</option>
            </select>
        </td>
        <td>应退金额</td>
        <td>
            <span id="amountPayable"></span>
        </td>
    </tr>
    <tr>
        <td>实退金额</td>
        <td>
            <span id="amountPaid"></span>
        </td>
        <td>收货日期</td>
        <td>
            <input type="datetime-local" step="1" id="returnDate">
        </td>
        <td>备注</td>
        <td>
            <textarea id="remarks" cols="30" rows="10"></textarea>
        </td>
    </tr>
    <tr>
        <td>是否付款</td>
        <td>
            <select id="state">
                <option value="1">已付</option>
                <option value="2">未付</option>
            </select>
        </td>
        <td>用户</td>
        <td>
            <span>${user.username}</span>
            <input type="hidden" value="${user.uid}" id="uid">

        </td>
        <td>选择商品</td>
        <td>
            <input type="button" value="选择商品" onclick="supplier()">
        </td>
    </tr>
</table>
<table id="table">
    <tr>
        <td>商品编号id</td>
        <td>商品编码</td>
        <td>商品名称</td>
        <td>库存数量</td>
        <td>采购价格</td>
        <td>采购数量</td>
        <td>采购总金额</td>
    </tr>
</table>
</body>
<script>
    //绑定地址
    function getBangDinag(){
        let supplierId = $("#supplierId option:selected").data("aa");
        $("#remarks").html(supplierId)
    }
        //日期
        let date = new Date();
        let fullYear = date.getFullYear();
        let month = date.getMonth()+1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();
        let number = Math.floor(Math.random()*9999-1000+1)+1000;
        let obj = "TH"+fullYear+month+day+hours+minutes+seconds+number;
        $("#returnNumber").html(obj);
    //绑定商品订单
    function supplier(){
        let supplierId = $("#supplierId").val();
        if(!supplierId){
            alert("请选择供应商");
            return;
        }
        let obj = {supplierId};
        $.ajax({
            url:"/goods/getGoodsSupplierId",
            type:"post",
            data:{reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                $("#table").empty();
                $("#table").append(`
                    <tr>
                        <td>
                            <input type="checkbox" id="check" onchange="getMoneys()">
                        </td>
                        <td>商品编号id</td>
                        <td>商品编码</td>
                        <td>商品名称</td>
                        <td>库存数量</td>
                        <td>采购价格</td>
                        <td>采购数量</td>
                        <td>总价格</td>
                    </tr>
                `)
                for (let o of res) {
                    $("#table").append(`
                        <tr>
                        <td>
                            <input type="checkbox" value="\${o.goodsId}" class="ck" onchange="getMoneys()">
                        </td>
                        <td>\${o.goodsId}</td>
                        <td>\${o.goodsCode}</td>
                        <td>\${o.goodsName}</td>
                        <td>\${o.inventoryQuantity}</td>
                        <td>\${o.purchasingPrice}</td>
                        <td>
                            <input type="text" id="num\${o.goodsId}" onchange="getGoodsNum(\${o.purchasingPrice},
                             this,\${o.goodsId},\${o.inventoryQuantity})">
                        </td>
                        <td>
                            <span id="sumPrice\${o.goodsId}"></span>
                        </td>
                    </tr>

                    `)
                }
                $("#table").append(`
                        <tr>
                            <td colspan="100">
                                <input type="button" value="确定退货出库" onclick="RemovePurchase()">
                                <input type="button" value="返回" onclick="history.back()">
                            </td>
                        </tr>
                    `)
            },
            error() {
                alert("服务器错误")
            }
        })
    }
    function getMoneys(){
        let count = 0;
        let sum = 0.0;
        $(".ck:checked").each(function (){
            let goodsId = $(this).val();
            let sumPrice = $("#sumPrice"+goodsId).html();
            let num = $("#num"+goodsId).val();
            count += +num;
            sum+= +sumPrice;
        })
        //赋值
        $("#amountPayable").html(sum);
        //实付金额默认原价 （应付金额）
        $("#amountPaid").html(sum);
    }
    function getGoodsNum(price,num,goodsId,count) {
        let n = $(num).val();
        if(n<=0 ||n>count){
            alert("数据无法出库");
            $(num).val('');
            return
        }
        $("#sumPrice"+goodsId).html((price*n).toFixed(2))
    }
    function RemovePurchase(){
        let returnNumber = $("#returnNumber").html();
        let supplierId = $("#supplierId").val();
        let amountPayable = $("#amountPayable").html();
        let amountPaid = $("#amountPaid").html();
        let returnDate = $("#returnDate").val();
        let remarks = $("#remarks").val();
        let state = $("#state").val();
        let uid = $("#uid").val();
        //获取 中间表 --> 只需要获取商品ID
        //获取选中的复选框
        let arr = [];
        $(".ck:checked").each(function (){
            let goodsId = $(this).val();
            let outNum = $("#num"+goodsId).val();
            let returnId = $(this).val();
            let obj = {goodsId,outNum,returnId};
            arr.push(obj);
            if(!arr.length){
                alert("请先选择要 购买的商品");
                return;
            }
        })
        if(!returnNumber|| !supplierId|| !amountPayable|| !amountPaid|| !returnDate|| !remarks|| !state|| !uid){
            alert("数据不能为空");
            return;
        }
        let  obj = {
            returnNumber,
            supplierId,
            amountPayable,
            amountPaid,
            returnDate,
            remarks,
            state,
            uid,
            arr
        }
        $.ajax({
            url:"/return/returnPurchase",
            type: "post",
            data:{reqInfos:JSON.stringify(obj)},
            dataType:"json",
            success(res){
                console.log(res);
                if(res === 200){
                    alert("添加成功");
                    location.href = "lists.jsp";
                }
            },
            error(){
                alert("服务器故障")
            }
        })



    }

</script>
</html>
